(function () {
    var core = ecui,
        util = ecui.util,
        dom = ecui.dom;

    ecui.util.extend(
        NS.data,
        {
            permission: {
                venue: {
                    'delete': ['preOrderDelete', 'fixedOrderDelete', 'roadShowOrderDelete'],
                    'audit': ['preOrderAuditFinal', 'fixedOrderAuditFirst', 'fixedOrderAuditFinal', 'roadShowOrderAuditFirst', 'roadShowOrderAuditFinal'],
                    'auditFirst': ['fixedOrderAuditFirst', 'roadShowOrderAuditFirst'],
                    'auditFinal': ['preOrderAuditFinal', 'fixedOrderAuditFinal', 'roadShowOrderAuditFinal'],
                    'modify': ['preOrderModify', 'fixedOrderModify', 'roadShowOrderModify']
                },
                contract: {
                    'delete': ['courtDelete', 'lottingDelete', 'commerceDelete', 'selfDelete'],
                    'audit': ['courtAuditFirst', 'courtAuditFinal', 'lottingAuditFirst', 'lottingAuditFinal', 'commerceAuditFirst', 'commerceAuditFinal', 'selfAuditFirst', 'selfAuditFinal'],
                    'auditFirst': ['courtAuditFirst', 'lottingAuditFirst', 'commerceAuditFirst', 'selfAuditFirst'],
                    'auditFinal': ['courtAuditFinal', 'lottingAuditFinal', 'commerceAuditFinal', 'selfAuditFinal'],
                    'modify': ['courtModify', 'lottingModify', 'commerceModify', 'selfModify']
                },
                paly: {
                    'delete': ['playDelete'],
                    'audit': ['playAuditFinal'],
                    'modify': ['playModify']
                },
                sponsor: {
                    'delete': ['sponsorListDelete'],
                    'audit': ['sponsorListAuditFinal'],
                    'modify': ['sponsorListModify']
                },
                musician: {
                    'delete': ['musicianListDelete'],
                    'audit': ['musicianListAuditFinal'],
                    'modify': ['musicianListModify']
                }
            }
        }
    );
    mao.joinPermission = function (module, type) {
        var moduleMap = ['', 'venue', 'contract', 'paly', 'sponsor', 'musician'];
        return NS.data.permission[moduleMap[module]][type].map(function (item) { return moduleMap[module] + ':' + item; }).join('||');
    };
    ecui.util.extend(NS.ui, {
        OrderCommentSave: core.inherits(
            mao.ui.CommentSave,
            {
                onsubmit: function () {
                    if (this._oRequest) {
                        return;
                    }
                    this._oRequest = true;
                    var form = this.getInput().form;
                    if (this.action === '7') {
                        // 评论
                        ecui.esr.request(
                            'data@JSON /erp-management/venue/order/comment?id=' + form.id.value + '&comment=' + form.actionRemark.value,
                            function () {
                                this._oRequest = false;
                                var data = ecui.esr.getData('data');
                                if (data instanceof Object) {
                                    ecui.tip('warn', '评论成功');
                                    ecui.esr.getRoute('detail.order').CACHE = false;
                                    ecui.esr.callRoute(util.stringFormat('detail.order~orderSn={0}~moduleType={1}~initiate={2}', ecui.esr.getData('orderSn'), ecui.esr.getData('moduleType'), ecui.esr.getData('initiate')));
                                }
                            }.bind(this),
                            function () {
                                this._oRequest = false;
                            }.bind(this)
                        );
                    } else if (this.action === '6') {
                        // 删除
                        ecui.esr.request(
                            'data@FORM /erp-management/venue/order/delete?' + form.name,
                            function () {
                                this._oRequest = false;
                                var data = ecui.esr.getData('data');
                                if (data instanceof Object) {
                                    ecui.tip('warn', '删除成功');
                                    ecui.esr.getRoute('/approval/initiate.initiate').CACHE = false;
                                    history.go(-1);
                                }
                            }.bind(this),
                            function () {
                                this._oRequest = false;
                            }.bind(this)
                        );
                    } else if (this.action === '5') {
                        // 编辑
                        this._oRequest = false;
                    } else {
                        // 0-撤销 1-提交 2-驳回 3-通过 4-中止
                        var tip = ['撤销', '提交', '驳回', '通过', '中止'];
                        ecui.esr.request(
                            'data@FORM /erp-management/venue/order/action?' + form.name,
                            function () {
                                this._oRequest = false;
                                var data = ecui.esr.getData('data');
                                if (data instanceof Object) {
                                    ecui.tip('warn', tip[+this.action] + '成功');
                                    if (ecui.esr.getData('initiate') === 'true') {
                                        ecui.esr.getRoute('/approval/initiate.initiate').CACHE = false;
                                    } else {
                                        ecui.esr.getRoute('/approval/approve').CACHE = false;
                                    }
                                    history.go(-1);
                                }
                            }.bind(this),
                            function () {
                                this._oRequest = false;
                            }.bind(this)
                        );

                    }
                }
            }
        )
    });
    ecui.esr.addRoute('order', {
        model: ['detail@GET /erp-management/venue/order/${id}?msgId=${msgId}'],
        weight: 4,
        main: 'approval_detail-order',
        view: 'detail.order',
        frame: true,
        CACHE: false,
        onbeforerequest: function (context) {
        },
        onbeforerender: function (context) {
            var title = ['', '的预定场申请', '的定场申请', '的巡演申请'];
            ecui.esr.setData('orderTitle', context.detail.creatorName + title[context.moduleType]);
            mao.util.setTitle(context.detail.creatorName + title[context.moduleType]);
            context.detail.actionCuttingName = context.detail.actionCutting.map(function (item) { return item.name; }).join('，');
            context.detail.musiciansName = context.detail.musicians.map(function (item) { return item.name; }).join('，');
            context.detail.deptName = (context.detail.dept || []).map(function (item) { return item.name; }).join(' - ');
        }
    });
}());